package com.ultrawise.survey.module.user.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.ultrawise.survey.contants.SurveyConstants;
import com.ultrawise.survey.core.base.BaseContoller;
import com.ultrawise.survey.core.base.ServiceException;
import com.ultrawise.survey.module.user.service.UsersService;
import com.ultrawise.survey.module.user.vo.TSurveyInfoUsers;

/**
 * 前台业务查询管理
 * 
 */
@Controller
@RequestMapping(value = "/")
public class UsersLogin extends BaseContoller {
	@Autowired
	private UsersService usersService;


	@RequestMapping(value = "aclogin.htm", method = RequestMethod.POST)
	public ModelAndView login(TSurveyInfoUsers userdto, HttpSession session,
			HttpServletRequest request) throws ServiceException {
		String randCode = request.getParameter("randCode");
		Map map = new HashMap();
		// 判断验证码
		if (session.getAttribute("rand") == null) {
			map.put("success", "false");
			map.put("msg", "您页面停留时间过长，请刷新页面重新登录。");
		} else {
			String _randCode = (String) session.getAttribute("rand");
			TSurveyInfoUsers formuser = (TSurveyInfoUsers) userdto;
			if (_randCode.equalsIgnoreCase(randCode)) {
				Object obj = usersService.queryByColunm("US_LOGIN",
						userdto.getUs_login(), userdto, TSurveyInfoUsers.class);
				if (obj != null) {// 用户存在
					TSurveyInfoUsers user = (TSurveyInfoUsers) obj;

					if (SurveyConstants.USER_STATUS_ISSTOP == user
							.getUs_status()) {
						// 该用户已经被停止使用
						map.put("success", "false");
						map.put("msg", "该用户已经被停止使用，请联系管理员。");
					} else if (user.getUs_passwd().equalsIgnoreCase(
							formuser.getUs_passwd())) {

						map.put("success", "true");
						map.put("msg", "身份验证成功.");
//						System.out.println("验证成功了..............loginsuccess:"
//								+ formuser.getUs_login());
						session.setAttribute(SurveyConstants.LOGINED_USER, user);
						session.setAttribute(
								SurveyConstants.LOGIN_SESSION_LOGINNAME,
								(user != null) ? user.getUs_login() : null);
					} else {
						map.put("success", "false");
						map.put("msg", "用户的密码输入错误，身份验证失败。");
					}
				} else {
					map.put("success", "false");
					map.put("msg", "用户名不存在，请核实是否输入错误，身份验证失败");
				}
			} else {
				// 图形验证码错误
				map.put("success", "false");
				map.put("msg", "图形验证码输入错误。");
			}
		}
		request.setAttribute("map", map);
		return new ModelAndView("/loadindex");
	}

	@RequestMapping(value = "login.htm")
	public ModelAndView login(HttpSession session) throws ServiceException {
		Map map = new HashMap();
		if(session.getAttribute(SurveyConstants.LOGINED_USER)!=null){
			map.put("success", "true");
			return new ModelAndView("/loadindex","map",map);
		}else
			return new ModelAndView("/user/index");
	}

	@RequestMapping(value = "logout.htm")
	public ModelAndView logout(HttpServletRequest request, HttpSession session)
			throws ServiceException {
		session.removeAttribute(SurveyConstants.LOGINED_USER);
		session.invalidate();
		Map map = new HashMap();
		map.put("success", "false");
		map.put("msg", "谢谢使用问卷系统，您已经正常退出。");
		request.setAttribute("map", map);
		return new ModelAndView("/user/index");
	}


}
